package com.example.timeplanning_teacher;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

public class ChangePsdActivity extends AppCompatActivity {
    private EditText et_password;//密码
    private EditText et_confirm_psd;
    private Handler handler;
    private String parent_name;
    private String username;
    private Button btn_confirm;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_psd);
        sp = getSharedPreferences("parent", Context.MODE_PRIVATE);
        username = sp.getString("username","");
        parent_name = sp.getString("parent_name","");
        et_password = findViewById(R.id.et_psd);
        et_confirm_psd = findViewById(R.id.et_confirmpsd);
        btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=et_password.getText().toString();//获取输入的密码
                String confirm_password=et_confirm_psd.getText().toString();//获取输入的确认密码
                if(!"".equals(username) && !"".equals(password)){
                    //判断两次输入的密码是否一致
                    if (!password.equals(confirm_password)) {
                        Toast.makeText(ChangePsdActivity.this, "两次输入的密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
                        et_confirm_psd.setText("");//清空“确认密码”编辑框
                        et_confirm_psd.requestFocus(); //让“密码”编辑框获得焦点
                    } else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    String json = "{\n" +
                                            "  \"parent_name\": \"" + parent_name + "\",\n" +
                                            "  \"username\": \"" + username + "\",\n" +
                                            "  \"password\": \"" + password + "\"\n" +
                                            "}";
                                    OkHttpClient client = new OkHttpClient();
                                    Request request = new Request.Builder()
                                            .url("http://58.58.36.198:9900/parent/update")
                                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                                            .build();
                                    Response response = client.newCall(request).execute();
                                    String responseData = response.body().string();
                                    JSONObject jsonObject = new JSONObject(responseData);
                                    int code = (int) jsonObject.get("code");
                                    if (code == 100) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                SharedPreferences.Editor editor = sp.edit();
                                                editor.putString("password", et_password.getText().toString());
                                                editor.commit();
                                                Message message = new Message();
                                                message.what = 1;
                                                handler.sendMessage(message);
                                                Toast.makeText(ChangePsdActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    } else if (0 == code) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(ChangePsdActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(ChangePsdActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        }).start();
                    }

                }else{
                    Toast.makeText(ChangePsdActivity.this,"请将注册信息输入完整！",Toast.LENGTH_SHORT).show();
                }
            }
        });

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        Intent intent=new Intent(ChangePsdActivity.this, FragmentActivity.class);
                        startActivity(intent);
                        ChangePsdActivity.this.finish();
                }
            };
        };
    }
}